define(["../common"], function() {
	
	var $form = $("#sgForm");
	var editDoPath = g_shopDomain + "/storeGoods/editDo/json";
	var picItemHtml = $("#picItemTmp").html();

	var formConfig = {
		fields: {
			title: "required; length[~80]",
			subTitle: "length[~80]",
			keywords: "required;length[~50]",
			status: "checked",
			'category.catId': {
				rule: 'required',
				msg: {required: "请选择类目"}
			},
			price: {
				rule: "required;money;range[~999999]",
				target: "#priceMsg"
			},
			originalPrice: {
				rule: "required;money;range[~999999]",
				target: "#originalPriceMsg"
			},
			selfCost: {
				rule: "required;money;range[~999999]",
				target: "#selfCostMsg"
			},
			unitConvert: {
				rule: "required;amount;range[0.01~];range[~99]",
				msg: {range: "只能是大于0的值"},
				target: "#unitConvertMsg"
			},
			stockEnd: {
				rule: "required;amount;range[~99]",
				target: "#stockEndMsg"
			},
			sellMin: {
				rule: "required;amount;range[~99]",
				target: "#sellMinMsg"
			},
			sellDef: {
				rule: "required;amount;match[gte, sellMin];range[~99]",
				msg: {match: "不能于小最小出售量"},
				target: "#sellDefMsg"
			},
			sellExt: "required;amount;range[~99]",
			description: "required"
		},
		valid: function(form) {
			if(!$(form).showMsg("正在保存...", 'load', true)) return;
			
			// 各级类名拼接
			var $selectedSelect = $("#selectCats select option:selected[value!='']");
			var catPath = $selectedSelect.map(function() {
				return $(this).val()?$(this).text():"";
			}).get().join(" > ");
			$("#categoryPath").val(catPath);
			
			$.post(editDoPath, $(form).serialize(), function(data) {
				if (data.status === 0) {
					$(form).showMsg("保存成功", 'pass');
					// 返回商品列表
				} else {
					$(form).showMsg(data.message, 'error');
				}
			});
		}
	};

	loadjs(["validator"], function() {
		$form.validator(formConfig);
	});
	loadjs(["category"], function() {
		$("#selectCats").categoryComboBox({
			value: $("#catId").val(),
			nullName: "-选择-",
			onSelect: function(deep, category) {
				$("#catId").val(category?category.catId:"");
				$("#catId").trigger('validate');
			}
		});
	});
	

	// 事件：提交
	$("[id^='submitBt_']").click(function() {
		$form.trigger('validate');
	});

	
	loadjs(["goodsClassify"], function() {
		$("#selectClassify").goodsClassifyComboBox({
			value: $("#classifyId").val(),
			nullName: "-选择-",
			onLoad: function($select) {
				$select.prop("disabled", true);
			}
		});
	});

	// 图片相关start#######################################

	var finder;
	require(["ckfinder"], function() {
		// 图片库
		finder = new CKFinder();
		finder.connectorInfo = "attaType=STORE";
		finder.resourceType = 'Images';
		finder.selectActionData = "container";
		finder.selectActionFunction = function(fileUrl, data) {
			var itemHtml = picItemHtml;
			if ($("#img_pic").length > 0) {
				itemHtml = itemHtml
					.replace(/\-\{num\}\-/g, parseInt(Math.random() * 1e10))
						.replace(/\-\{url\}\-/g, fileUrl);
			} else {
				$("#pic").val(fileUrl);
				itemHtml = "<img id='img_pic' data='" + fileUrl + "' ><i>默认</i>";
			}
			$("#bt_addPic").before(itemHtml);
			$(window).scroll();// 触发显示图片
			
			$("#picCount").html(parseInt($("#picCount").html()) - 1);
		};
	});
	

	// 事件：添加图片
	$("#bt_addPic").click(function() {
		// 图片数量
		if (parseInt($("#picCount").html()) === 0) {
			$(this).showMsg("图片数量已达上限", 'error');
		} else {
			finder.popup();		
		}
	});
	
	// 设置默认图片
	$(document).on('click', "[id^='img_pic_']", function() {
		var num = $(this).attr("id").split("_")[2];
		var pic = $("#pic").val();
		var img = $("#img_pic").attr("src");
		
		$("#pic").val($("#pic_" + num).val());
		$("#img_pic").attr("src", $("#img_pic_" + num).attr("src"));
		
		$("#pic_" + num).val(pic);
		$("#img_pic_" + num).attr("src", img);
	});

	// 事件：删除图片
	$(document).on("click", "[id^='picDel_']", function() {
		var $this = $(this);
		var num = $this.attr("id").split("_")[1];
		$("#pic_" + num).remove();
		$("#img_pic_" + num).remove();
		$this.remove();
		
		$("#picCount").html(parseInt($("#picCount").html()) + 1);
	});

});
